package com.be.beadmin.service.util;

import com.be.beadmin.common.exception.BeadminException;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 18:34 2023/2/9
 * @Modified By:
 **/
public class DateUtil {
    /**
     * 获取指定日期的前或后几小时
     * @param inputDate 指定日期
     * @param hours 小时数，正表示向后，负表示向前
     * @return
     */

    public static Date computeDate(Date inputDate,int hours){

//        Date today = new Date();//获取今天的日期

        Calendar c = Calendar.getInstance();

        c.setTime(inputDate);

        c.add(Calendar.HOUR_OF_DAY, hours);

        return c.getTime();//这是


    }
    public static Date transDateFormat(Date date,String format){

        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date1;
        try {
            date1 = df.parse(df.format(date));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date1;
    }
    public static Float computeDateDuring(Date startDate,Date endDate){ // 分钟计算
        if (startDate.after(endDate)) throw new BeadminException("开始时间在结束时间之后了");
        long re = (endDate.getTime() - startDate.getTime()) / 1000 / 60;
        return (float)((endDate.getTime() - startDate.getTime()) / 1000 % 60 == 0 ? re : re+1);
    }

    public static Float computeDateDuringBySecond(Date startDate,Date endDate){ // 分钟计算
        if (startDate.after(endDate)) throw new BeadminException("开始时间在结束时间之后了");
        long re = (endDate.getTime() - startDate.getTime()) / 1000 ;
        return (float)((endDate.getTime() - startDate.getTime()) / 1000  == 0 ? re : re+1);
    }


    public static String transDateFormatToString(Date date){

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        return df.format(date);
    }

    public static String transDateFormatToString(Date date,String format){

        SimpleDateFormat df = new SimpleDateFormat(format);

        return df.format(date);
    }
    public static Date transDateFormat(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = df.parse(df.format(date));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date1;
    }

    public static Date StringToDate(String dateString){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = df.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    public  static String getNow(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String now = df.format(System.currentTimeMillis());
        return now;
    }

    public  static Float transMinToHour(Float min){
        if (min == null) return (float) 0;
        BigDecimal bigDecimal = new BigDecimal(min / 60).setScale(1, BigDecimal.ROUND_HALF_UP);
        return bigDecimal.floatValue();
    }

    public static void main(String[] args) {
        Date date =new Date();
        System.out.println(DateUtil.computeDate(date,-7*24));
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        String now3 = df.format(System.currentTimeMillis());
//        System.out.println("now3:" + now3);


    }

}
